package com.hengpeng.itfinoss.service.article.impl;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.ArticleCategory;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.article.ArticleCategorySerivce;

/**
 * <pre>
 * TODO 文章类型服务接口实现
 * </pre>
 * @author LMM
 * @version 1.0, 2011-8-31
 */
@Service
@Lazy
public class ArticleCategorySerivceImpl implements ArticleCategorySerivce
{

	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<ArticleCategory> delete(Set<Object> ids)
	{
		for (Object id : ids)
		{
			serviceFactory.getPersistService().delete(ArticleCategory.class, id);
		}
		return new ServiceResult<ArticleCategory>();
	}

	@Override
	public ServiceResult<List<ArticleCategory>> findAll()
	{
		List<ArticleCategory> list = serviceFactory.getPersistService().list(ArticleCategory.class);
		return new ServiceResult<List<ArticleCategory>>(list);
	}

	@Override
	public ServiceResult<ArticleCategory> get(Long id)
	{
		ArticleCategory articleCategory = serviceFactory.getPersistService().get(ArticleCategory.class, id);
		return new ServiceResult<ArticleCategory>(articleCategory);
	}

	@Override
	public ServiceResult<ArticleCategory> load(Long id)
	{
		ArticleCategory articleCategory = serviceFactory.getPersistService().get(ArticleCategory.class, id);
		return new ServiceResult<ArticleCategory>(articleCategory);
	}

	@Override
	public ServiceResult<ArticleCategory> save(ArticleCategory articleCategory)
	{
		ArticleCategory obj = serviceFactory.getPersistService().save(articleCategory);
		return new ServiceResult<ArticleCategory>(obj);
	}

	@Override
	public ServiceResult<ArticleCategory> update(ArticleCategory articleCategory)
	{
		ArticleCategory obj = serviceFactory.getPersistService().update(articleCategory);
		return new ServiceResult<ArticleCategory>(obj);
	}

	@Override
	public ServiceResult<Long> findArticleCategoryCount(String name, Long parent)
	{
		DynamicQuery pagination = new DynamicQuery(ArticleCategory.class);
		if (!StringHelper.isEmpty(name))
			pagination.like("name", "%" + name + "%");
		if (parent != null && !parent.equals(Constant.G_LONG_DATA_INVALID))
			pagination.eq("parent.id", parent);
		return new ServiceResult<Long>(serviceFactory.getPersistService().findPageCount(pagination,null));
	}

	@Override
	public ServiceResult<List<ArticleCategory>> findArticleCategoryPage(String name, Long parent, Integer pageIndex,
			Integer pageSize, LinkedHashMap<String, OrderType> sortMap)
	{

		DynamicQuery pagination = new DynamicQuery(ArticleCategory.class);
		if (!StringHelper.isEmpty(name))
			pagination.like("name", "%" + name + "%");
		if (parent != null && !parent.equals(Constant.G_LONG_DATA_INVALID))
			pagination.eq("parent.id", parent);
		pagination.setPageIndex(pageIndex);
		pagination.setPageSize(pageSize);
		if (sortMap != null)
		{
			for (Entry<String, OrderType> entry : sortMap.entrySet())
			{
				if (entry.getValue() == OrderType.DESC)
				{
					pagination.desc(entry.getKey());
				}
				else
				{
					pagination.asc(entry.getKey());
				}
			}
		}
		return new ServiceResult<List<ArticleCategory>>(serviceFactory.getPersistService().find(ArticleCategory.class,
				pagination));
	}

	@Override
	public ServiceResult<Set<ArticleCategory>> findAllArticleCategorySort(Long categoryId)
	{
		/* 顶级分类 */
		List<ArticleCategory> list = findLevelArticleCategory(null);
		Set<ArticleCategory> set = new LinkedHashSet<ArticleCategory>();
		iterator(list, set, categoryId);
		return new ServiceResult<Set<ArticleCategory>>(set);
	}

	private void iterator(List<ArticleCategory> list, Set<ArticleCategory> category, Long categoryId)
	{
		if (list != null && list.size() > 0)
		{
			for (ArticleCategory categorys : list)
			{
				if (categoryId != null && categorys.getId().equals(categoryId))
				{
					continue;
				}
				category.add(categorys);
				List<ArticleCategory> subList = findLevelArticleCategory(categorys.getId());
				iterator(subList, category, categoryId);
			}
		}
	}

	public ServiceResult<List<ArticleCategory>> findChildByIds(Long[] ids)
	{
		DynamicQuery pagination = new DynamicQuery(ArticleCategory.class);
		pagination.inArray("parent.id", ids);
		List<ArticleCategory> list = serviceFactory.getPersistService().find(ArticleCategory.class, pagination);
		return new ServiceResult<List<ArticleCategory>>(list);
	}

	@Override
	public ServiceResult<List<ArticleCategory>> findArticleCategoryByIds(Long[] ids)
	{
		DynamicQuery pagination = new DynamicQuery(ArticleCategory.class);
		pagination.inArray("id", ids);
		List<ArticleCategory> list = serviceFactory.getPersistService().find(ArticleCategory.class, pagination);
		return new ServiceResult<List<ArticleCategory>>(list);
	}

	public List<ArticleCategory> findLevelArticleCategory(Long level)
	{
		DynamicQuery pagination = new DynamicQuery(ArticleCategory.class);
		if (level == null)
		{
			pagination.isNull("parent");
		}
		else
		{
			pagination.eq("parent.id", level);
		}
		return serviceFactory.getPersistService().find(ArticleCategory.class, pagination);
	}

}
